package ga_yad2;

import genetic_algorithm.Chromosome;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import utils.RandomGenerator;

public class Yad2Chromosome implements Chromosome {

	public static final int START = 0;
	public static final int AMOUNT = 1;

	public static List<int[]> productsSplit;
	public static List<Character> allProducts;
	List<Character> allocation;

	public static void initProducts(List<int[]> splitIndexes,
			List<Character> all) {
		
		productsSplit = splitIndexes;
		allProducts = all;
	}

	public Yad2Chromosome(List<Object> allocationObjs) {
		
		super();
		this.allocation = new LinkedList<Character>();
		for (Object o : allocationObjs) {
			// allocation.add((Character) o); // TODO CHECK IF BETTER
			allocation.add(new Character((Character) o));
		}
	}

	public Yad2Chromosome() {
		
		List<Character> tempAllProducts = new LinkedList<Character>(allProducts);
		LinkedList<Character> values = new LinkedList<Character>();
		while (!tempAllProducts.isEmpty()) {
			int index = RandomGenerator.nextInt(tempAllProducts.size());
			values.add(tempAllProducts.get(index));
			tempAllProducts.remove(index);
		}
		allocation = values;
	}
	
	@Override
	public Character getValue(int index) throws IllegalArgumentException {

		try {
			return allocation.get(index);
		} catch (Exception e) {
			return null;
		}
	}

	@Override
	public void setValue(int index, Object obj) throws IllegalArgumentException {
		allocation.set(index, (Character) obj);

	}

	@Override
	public List<Object> getAllValues() {
		return new LinkedList<Object>(allocation);
	}

	// TODO CHECK IF SHOULD BE DELETED
	public String toString() {

		// initialize output string
		String output = "";

		// for each person
		for (int currPerson = 0; currPerson < Yad2Chromosome.productsSplit
				.size(); ++currPerson) {
			// get all products owned by current person
			output += "\nperson #" + currPerson + "'s products: ";
			int offset = Yad2Chromosome.productsSplit.get(currPerson)[Yad2Chromosome.START];
			int numProducts = Yad2Chromosome.productsSplit.get(currPerson)[Yad2Chromosome.AMOUNT];
			for (int currProduct = offset; currProduct < offset + numProducts; ++currProduct) {
				output += allocation.get(currProduct) + ", ";
			}
		}

		return output;
	}
}
